
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SchedulerGUI {
	private JFrame mainWindow;	
	private final int WINDOW_WIDTH;
	private final int WINDOW_HEIGHT;
	private JPanel infoPanel;		// panel to contain system message
	private JTextField systemMessage;
	private Scheduler myScheduler;
	private Checkbox[] neighborList;

	/**
	 * constructor for checker GUI class
	 */
	public SchedulerGUI(Scheduler newScheduler){
		WINDOW_WIDTH =600;	// width of the window
		WINDOW_HEIGHT=400;	// height of the window
		mainWindow = new JFrame("Meeting Scheduler");	// name of the window
		infoPanel= new JPanel();
		myScheduler = newScheduler;
		neighborList = new Checkbox[0];
		mainWindow();	// set up main window
		Component mainWindowContents = createMainWindowContent();
		//mainWindowContents.setVisible(true);
		mainWindow.add(mainWindowContents,BorderLayout.CENTER);
		mainWindow.setVisible(true);	//set the window to be visible

	}

	/**
	 * set up main window
	 */
	public void mainWindow(){
		// set up for new game window
		mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	// terminate the program when the window closes
		mainWindow.setSize(WINDOW_WIDTH,WINDOW_HEIGHT);		// set width and height for the window
		mainWindow.setLocation(500, 250);	// set where the window pops up on the screen
	}

	/**
	 * create the contents in the main window
	 * @return component which is the contents in the main window
	 */
	public Component createMainWindowContent (){
		infoLayer();	// setup information layer
		JPanel buttonLayer = buttonLayer();	// set up button layer		

		JPanel mainPanel = new JPanel();	// main panel to hold button layer, information layer, picture layer
		mainPanel.setLayout(new BorderLayout());	// set main panel to be border layout

		mainPanel.add(buttonLayer,BorderLayout.NORTH);	// buttonLayer to the north

		mainPanel.add(infoPanel,BorderLayout.CENTER);	// information panel to the center
		return mainPanel;


	}

	/**
	 * set up button layer
	 */
	public JPanel buttonLayer(){		
		JPanel buttonLayer = new JPanel();
		JPanel neighborListPanel = new JPanel();
		neighborListPanel.setLayout(new GridLayout(((int)(myScheduler.getNeighborList().size()/3)+1),2));
		buttonLayer.setLayout(new GridLayout(1,2));
		JButton createMeetingButton = new JButton("Create meeting"); // button for create meeting
		createMeetingButton.addActionListener(new CreateMeetingButtonListener());
		buttonLayer.add(createMeetingButton);

		neighborList = new Checkbox[myScheduler.getNeighborList().size()];
		for(int i=0;i<neighborList.length;i++){
			neighborList[i] = new Checkbox(myScheduler.getNeighborList().get(i).getHostName(),false);
			neighborListPanel.add(neighborList[i]);
		}
		buttonLayer.add(neighborListPanel);
		return buttonLayer;

	}

	private class CreateMeetingButtonListener implements ActionListener{
		private String memberList;
		public CreateMeetingButtonListener(){
			memberList="";
		}
		@Override
		public void actionPerformed(ActionEvent arg0) {
			if(!myScheduler.getLockStatus()){	//not participating another scheduling
				for(int i=0;i<neighborList.length;i++){
					if(neighborList[i].getState()){	// if it is check
						if(memberList.compareTo("")!=0) // not the first element to insert
							memberList = memberList.concat(",");
						memberList=memberList.concat(myScheduler.getNeighborList().get(i).getHostAddress());
					}
				}
				if(memberList.compareTo("")!=0)
					myScheduler.createMeeting(memberList);
				else
					updateSystemMessage("need at least one member to start a scheduling");

			}
			else
				updateSystemMessage("participating another scheduling");
		}
	}

	/**
	 * set up information layer
	 */
	public void infoLayer(){

		//turnInfo.setVisible(true);

		systemMessage = new JTextField ("System message",50);	// display system message
		Font font = new Font("VerDana",Font.BOLD,20);
		systemMessage.setFont(font);
		systemMessage.setForeground(Color.BLACK);
		systemMessage.setHorizontalAlignment(JTextField.CENTER);	// set central alignment
		systemMessage.setEditable(false);	// make the text field uneditable
		systemMessage.setSize((2*WINDOW_WIDTH)/3, WINDOW_HEIGHT/5);
		//systemMessage.setVisible(true);

		infoPanel.setLayout(new BorderLayout());	// set the information panel to boarder layout
		infoPanel.add(systemMessage,BorderLayout.CENTER);	// set the system message textfield on the center
		infoPanel.setVisible(true);
	}

	/**
	 * update the message on the display
	 * @param newMessage message to be displayed
	 */
	public void updateSystemMessage(String newMessage){
		systemMessage.setText(newMessage);
	}
}
